<?php

/**
 * Customer Controller
 *
 * @author
 * @version
 */
require_once   BaseUtil::getModulePath("base")  .  '/controllers/BaseController.php';
require_once   BaseUtil::getModulePath("customer")  .  '/services/CustomerService.php';
require_once   BaseUtil::getModulePath("customer")  .  '/services/AgentService.php';
require_once   BaseUtil::getModulePath("customer")  .  '/forms/CustomerForm.php';
require_once   BaseUtil::getModulePath("common")  .  '/services/CommonService.php';
require_once   BaseUtil::getModulePath("customer")  .  '/forms/CommentForm.php';
require_once BaseUtil::getModulePath("customer") . '/constants/CustomerConstant.php';


class Customer_CustomerController extends BaseController {

	public function init(){
        parent::init();
        $this->view->script
            = array('customer.js','lib/ui.dialog.js','lib/ui.draggable.js',
                    'popup.js', 'inventory.js');
        $this->view->link = array('popup.css');
	}

    /*public function indexAction(){
        $customerService = new CustomerService();
        $customers = $customerService->getLastCustomerByLimit(10);
        $this->view->customers = $customers;
    }

    public function getlastAction(){
        $this->_helper->layout->disableLayout();
        $customerService = new CustomerService();
        $customers = $customerService->getLastCustomerByLimit(10);
        $this->view->customers = $customers;
    }*/
    
    public function listAction(){
		$customerService = new CustomerService();
        $customers = $customerService->getCustomers($this->view, $_POST);
		$this->view->customers = $customers;
    }

    public function ajaxsearchAction(){
        $this->_helper->layout->disableLayout();
 		$customerService = new CustomerService();
        $customers = $customerService->getCustomers($this->view, $_POST,true);
        echo $customers;
        exit();
    }

    public function addAction(){
        $this->_helper->layout->disableLayout();
        $customerForm = new CustomerForm();
        $this->view->form = $customerForm;
    }

    public function subaddAction(){
        $this->_helper->layout->disableLayout();
        $customerForm = new CustomerForm();
        $customerService = new CustomerService();
		if (!$customerForm->isValid($_POST)) {
			$this->view->form = $customerForm;
			return $this->render('add');
		}
        $customerService->addCustomer($customerForm);
        //$this->_redirect('/customer/customer');
        echo 'true';
        exit();
    }

    public function editAction(){
        $this->_helper->layout->disableLayout();
        $customerId = $this->_getParam('id');
        $customerService = new CustomerService();
        $customer = $customerService->getCustomerById($customerId);
        $customerForm = new CustomerForm(true);
//High School Info
        $commonServ = new CommonService();
        $this->view->province = $commonServ->getAllProvince();
        $this->view->campuses = $commonServ->getCampusByDictId(1);       
        $campus = $commonServ->getCampusById($customer['campus_id']);
        if($campus){
            $this->view->campus = $campus['area_name'];
        }
        
        $customerForm->getElement("id")->setValue($customer['id']);
        $customerForm->getElement("name")->setValue($customer['name']);
        $customerForm->getElement("email")->setValue($customer['email']);
        $customerForm->getElement("status")->setValue($customer['status']);
        $customerForm->getElement("userType")->setValue($customer['certf']);
       // if($customer['certf']==3){
       // 	$customerForm->getElement("userType")->setAttrib('disabled','disabled');
       // 	$customerForm->getElement("userType")->setAttrib('style','background-color:#fff; color:#606060;');
      //  }
        $customerForm->getElement("teamName")->setValue($customer['team_name']);
        $customerForm->getElement("fullName")->setValue($customer['full_name']);
        $customerForm->getElement("province")->setValue($customer['district']);
        $customerForm->getElement("city")->setValue($customer['city']);
        $customerForm->getElement("receiveAddr")->setValue($customer['addr']);
        $customerForm->getElement("zipCode")->setValue($customer['postal_code']);
        $customerForm->getElement("mobile")->setValue($customer['mobile']);
        $customerForm->getElement("phone")->setValue($customer['phone']);
        $customerForm->getElement("pointsBuyer")->setValue($customer['points_buyer']);
      //  $customerForm->getElement("pointsSeller")->setValue($customer['points_seller']);
        $customerForm->getElement("pointsBuyerTotal")->setValue($customer['points_buyer_total']);
       // $customerForm->getElement("pointsSellerTotal")->setValue($customer['points_seller_total']);
        $customerForm->getElement("customerRank")->setValue(CustomerConstant::getCustomerRank($customer['rank']));
        $customerForm->getElement("lastIp")->setValue($customer['last_ip']);
        $customerForm->getElement("lastLogin")->setValue($customer['last_login']);
        $this->view->form = $customerForm;
        $this->view->id=$customerId;
        $this->view->userType = $customer['certf'];
        $this->view->campusId = $customer['campus_id'];
        if($customer['certf']==3){
            $agentService = new AgentService();
            $accountAgent = $agentService->getAgentTotalByUserID($customerId);
            $this->view->accountAgent = $accountAgent;
        }
        
        // customer wishlists
        $totalNum = $customerService->getWishListTotalNum($customerId);
        $paginate = BaseUtil::paginate(1,$totalNum);
      
        $customerWishlists = $customerService->getWishlistByCustomerId($customerId,$paginate['offset'],$paginate['num_of_page']);
        $this->view->customerWishlists = $customerWishlists;
        $this->view->paginate = $paginate;
    }

    public function subeditAction(){
        $this->_helper->layout->disableLayout();
        $id = HttpUtil::getPostString("id");
        $customerForm = new CustomerForm(true,$id);
        $customerService = new CustomerService();
		if (!$customerForm->isValid($_POST)) {
			$this->view->form = $customerForm;
			return $this->render('edit');
		}
		
        $customerService->editCustomer($customerForm);
        //$this->_redirect('/customer/customer');
        echo "true";
        exit();
    }

    public function deletesomeAction(){
        $idsStr = HttpUtil::getPostValue("selectIds");
        $ids = explode(",",$idsStr);
		$customerService = new CustomerService();
		$customerService->deleteCustomers($ids);
		$this->_redirector->gotoUrl('/customer/customer/list');
    }
    
    public function deleteAction() {
    	$id=$this->_getParam('id');
    	$customerService=new CustomerService();
    	$customerService->delete($id);
    	//$this->_redirector->gotoUrl('/customer/customer/list');
        echo "true";
        exit();
    }
    
    public function checknameuniqueAction(){
    	$id = HttpUtil::getPostString('id');
    	$name=HttpUtil::getPostString('name');
    	
    	$customerService = new CustomerService();
    	$user = $customerService->checkUserByName($name,$id);
    	if(!$user){
    		$data = array('true','true');
    		echo json_encode($data);
    		exit();
    	} else {
    		$data = array('false','true');
    		echo json_encode($data);
    		exit();
    	}
    }
    
    public function checkemailuniqueAction(){
        $id = HttpUtil::getPostString('id');
    	$email = HttpUtil::getPostString('email');
    	
    	$customerService = new CustomerService();
        $user = $customerService->checkUserByEmail($email,$id);
    	
    	if(!$user){
    		$data = array('true','true');
    		echo json_encode($data);
    		exit();
    	} else {
    		$data = array('true','false');
    		echo json_encode($data);
    		exit();
    	}
    }
    
    public function agentrelationAction(){
        require_once   BaseUtil::getModulePath("customer")  .  '/services/AgentService.php';
        $agentServ = new AgentService();
        $this->view->customers = $agentServ->getAgentRelations($this->view, $_POST);
    }
    
    public function usercommentslistAction(){
    	$customerServ = new CustomerService();
    	$this->view->customers = $customerServ->getUserCommentsList($this->view, $_POST);
    }

    public function savecommentsAction(){
    	$replay['comment_id'] = HttpUtil::getPostString('id');
    	$replay['content'] = HttpUtil::getPostString('rcontent');
    	$admin = BaseUtil::getLoginUser();
    	$replay['reply_id'] = $admin['uid'];
        $customerServ = new CustomerService();
        $customerServ->saveCommentReply($replay);
    	$this->_redirect('/customer/customer/usercommentslist');
    }
    
    public function editcommentsAction(){
    	$commentId = $this->_getParam('id');
        $customerService = new CustomerService();
        $comment = $customerService->getCommentById($commentId);
        $commentForm = new CommentForm($comment);
        $commentForm->getElement("id")->setValue($comment['id']);
        $this->view->form = $commentForm;
        $this->view->comment=$comment;
      
        $this->view->id = $commentId;
    }
    
    public function deletecommentAction(){
    	$id = $this->_getParam('id');
    	$customerService = new CustomerService();
    	$customerService->deleteComment($id);
    	$this->_redirect('/customer/customer/usercommentslist');
    	
    }
    
    public function searchcustomerAction(){
    	$from = $this->_getParam("from");
        $this->_helper->layout->disableLayout();
        $customerService = new CustomerService();
        $customers = $customerService->searchCustomer($this->view, $_POST,$from);
        $this->view->customers = $customers;
    }
    
    public function checkagencyAction(){
    	$userId = HttpUtil::getPostString('userId');
    	$customerService = new CustomerService();
    	$agents = $customerService->getUsersByPId($userId);
    	if(!empty($agents)){
    		echo 1;
    		exit() ;
    	} else {
    		echo 0;
    		exit() ;
    	}
    }
    
    public function paginatewishlistAction(){
    	$this->_helper->layout->disableLayout();
    	$curPage = HttpUtil::getPostString('curPage');
    	$customerId = HttpUtil::getPostString('customerId');
    	$customerService = new CustomerService();
    	$totalNum = $customerService->getWishListTotalNum($customerId);
    	
        $paginate = BaseUtil::paginate($curPage,$totalNum);
      
        $customerWishlists = $customerService->getWishlistByCustomerId($customerId,$paginate['offset'],$paginate['num_of_page']);
        $this->view->id=$customerId;
        $this->view->customerWishlists = $customerWishlists;
    	$this->view->paginate = $paginate;
    }
    
    public function addcommentAction(){
        $addForm = new CommentAddForm();
        $this->view->form = $addForm;
    }
    
    public function subaddcommentAction()
    {
        $addForm = new CommentAddForm();
        $customerService = new CustomerService();
        if(!$addForm->isValid($_POST)){
            $this->view->form=$addForm;
            return $this->render('addcomment');
        }
        $customerService->addComment($addForm);
        $this->_redirect('/customer/customer/usercommentslist');
        exit();
    }
        
}